<template>
	<a-card :bordered="false" :body-style="{ 'padding-bottom': '0px' }" class="mb-2">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="8">
					<a-form-item label="关键词" name="searchKey">
						<a-input v-model:value.trim="searchFormState.searchKey" placeholder="请输入单位名称或编码"></a-input>
					</a-form-item>
				</a-col>
				<a-col :span="8">
					<a-form-item label="状态" name="state">
						<a-select v-model:value="searchFormState.state" placeholder="请选择状态" allowClear>
							<a-select-option value="1">启用</a-select-option>
							<a-select-option value="0">停用</a-select-option>
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="8">
					<a-button type="primary" @click="table.refresh(true)">查询</a-button>
					<a-button style="margin: 0 8px" @click="() => searchFormRef.resetFields()">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
	</a-card>
	<a-card :bordered="false">
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()">
						<template #icon>
							<plus-outlined/>
						</template>
						新增
					</a-button>
					<a-button danger @click="deleteBatchExtOrg()">删除</a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'state'">
					<a-switch
						:loading="loading"
						:checked="record.state === 1"
						@change="editStatus(record)"
					/>
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="formRef.onOpen(record)">编辑</a>
						<a-divider type="vertical"/>
						<a-popconfirm title="确定要删除吗？" @confirm="deleteExtOrg(record)">
							<a-button type="link" danger size="small">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="table.refresh(true)"/>
</template>

<script setup name="extOrg">
	import {message} from 'ant-design-vue'
	import Form from './form.vue'
	import bizOrgApi from '@/api/biz/bizOrgApi'

	let searchFormState = reactive({orgType:1})
	const table = ref()
	const formRef = ref()
	const searchFormRef = ref()
	const toolConfig = {refresh: true, height: true, columnSetting: true, striped: false}
	const columns = [
		{
			title: '单位编码',
			dataIndex: 'code'
		},
		{
			title: '单位名称',
			dataIndex: 'name'
		},
		{
			title: '单位地址',
			dataIndex: 'address'
		},
		{
			title: '排序码',
			dataIndex: 'sortCode'
		},
		{
			title: '状态',
			dataIndex: 'state'
		},
	]
	// 操作栏
	columns.push({
		title: '操作',
		dataIndex: 'action',
		align: 'center',
		width: '150px'
	})
	const loading = ref(false)

	let selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		alert: {
			show: false,
			clear: () => {
				selectedRowKeys = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const loadData = (parameter) => {
		return bizOrgApi.orgPage(Object.assign(parameter, searchFormState)).then((data) => {
			return data
		})
	}
	// 删除
	const deleteExtOrg = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		bizOrgApi.orgDelete(params).then(() => {
			message.success('删除成功')
			table.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchExtOrg = () => {
		if (selectedRowKeys.value.length < 1) {
			message.warning('请选择一条或多条数据')
			return false
		}
		const params = selectedRowKeys.value.map((m) => {
			return {
				id: m
			}
		})
		bizOrgApi.orgDelete(params).then(() => {
			message.success('批量删除成功')
			table.value.clearRefreshSelected()
		})
	}

	// 修改状态
	const editStatus = (record) => {
		loading.value = true
		bizOrgApi.orgSwitchState(record).then(() => {
			message.success('修改状态成功')
			loading.value = false
			table.value.refresh()
		})
	}
</script>
